package com.tarena.lbs.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@RestController
@Tag(name = "后台用户接口", description = "处理和后台用户相关的业务接口")
public class AdminController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private AdminService adminService;


    @Operation(summary = "管理员登录")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin(HttpServletRequest request) throws BusinessException {
        AdminLoginParam param = parseLoginParam(request);
        log.debug("管理员登录业务处理,入参:{}",param);
        LoginVO vo = loginService.doLogin(param);
        log.debug("管理员登录成功，入参:{},出参:{}",param,vo);
        return new Result<>(vo);
    }
    
    /**
     * 解析登录参数，支持表单数据和JSON数据
     */
    private AdminLoginParam parseLoginParam(HttpServletRequest request) throws BusinessException {
        String contentType = request.getContentType();
        AdminLoginParam param = new AdminLoginParam();
        
        try {
            if (contentType != null && contentType.contains("application/json")) {
                // 处理JSON数据
                ObjectMapper objectMapper = new ObjectMapper();
                param = objectMapper.readValue(request.getInputStream(), AdminLoginParam.class);
            } else {
                // 处理表单数据
                param.setPhone(request.getParameter("phone"));
                param.setPassword(request.getParameter("password"));
            }
        } catch (Exception e) {
            throw new BusinessException("-2", "参数解析失败: " + e.getMessage());
        }
        
        // 参数验证
        if (param.getPhone() == null || param.getPassword() == null) {
            throw new BusinessException("-2", "手机号和密码不能为空");
        }
        
        return param;
    }

    @Operation(summary = "用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail() throws BusinessException {
        log.debug("查询登录用户详情");
        AdminVO vo = adminService.detail();
        log.debug("查询登录用户详情成功，出参:{}",vo);
        return new Result<>(vo);
    }


    /**
     * 查询后台管理的 账号列表
     */
    @Operation(summary = "账号列表")
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query)
        throws BusinessException{
        PageResult<AdminVO> voPage = adminService.pageList(query);
        return new Result<>(voPage);
    }

    @Operation(summary = "账号保存")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param)
        throws BusinessException{
        log.debug("开始保存管理员账号，入参：{}", param);
        adminService.save(param);
        return Result.success();
    }













































}